<template>
  <div class="tab-nav-container">
    <!-- 上部分：hover显示对应的children -->
    <div class="hover-menu" v-if="currentChildren">
      <div v-for="item in currentChildren" :key="item.id" class="hover-item" @mouseenter="handleHover(item.id)"
        @mouseleave="handleHoverLeave" @click="gotoUrl(item)">
        <router-view />
        <div class="menu-icon" :class="{ 'is-hovered': hoveredItem === item.id }">
          <!-- 只保留切换逻辑，不管图标类型 -->
          <img :src="hoveredItem === item.id
            ? item.icon
            : hoveredItem === item.id
              ? item.hoverimg
              : item.checkoutimg" alt="菜单图标">
        </div>
        <span class="menu-text">{{ item.name }}</span>
      </div>
    </div>

    <!-- 下部分保持不变 -->
    <div class="click-menu">
      <div v-for="item in bottomMenus" :key="item.id" class="click-item"
        :class="{ 'is-active': activeItem === item.id }" @click="handleClick(item)"
        @mouseenter="handleClickHover(item.id)" @mouseleave="handleLeaveHover()">
        <img :src="activeItem === item.id
          ? item.icon
          : isHoveringId === item.id
            ? item.hoverimg
            : item.checkoutimg
          " alt="">
        <span class="menu-text">{{ item.name }}</span>
      </div>
    </div>
    <div class="footer">
      <span style="font-size: 10px;">美商智联网络科技南京有限公司2024 MEICCHACHA
        ALLRIGHTS保留。苏ICP备2021037182号</span>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import bottomMenusOption from './data.js'
import { useRouter } from 'vue-router';
const router = useRouter()
// 当前hover的项ID（上部分）- 初始值设为null，避免默认匹配
const hoveredItem = ref<number | null>(null)
// 当前激活的项（点击选中）
const activeItem = ref(1)
// 记录下部分当前鼠标悬停的id
const isHoveringId = ref<number | null>(null)

const bottomMenus = bottomMenusOption

// 当前选中项的children
const currentChildren = computed(() => {
  const active = bottomMenus.find(item => item.id === activeItem.value)
  return active?.children || []
})

// 上部分hover处理 - 直接接收id，避免对象引用问题
const handleHover = (id: number) => {
  hoveredItem.value = id
}
// 离开时重置为null
const handleHoverLeave = () => {
  hoveredItem.value = null
}

// 下部分点击处理
const handleClick = (item: any) => {
  activeItem.value = item.id
}

// 下部分hover处理
const handleClickHover = (id: number) => {
  if (activeItem.value !== id) {
    isHoveringId.value = id
  }
}
const handleLeaveHover = () => {
  isHoveringId.value = null
}

const gotoUrl = (item) => {
     if(item.url) return router.push(item.url)
             
}

</script>

<style scoped>
/* 原有样式保持不变，仅强化hover状态的视觉反馈 */
.tab-nav-container {
  display: flex;
  flex-direction: column;
  gap: 30px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 12px;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  position: relative;
  overflow: hidden;
}

/* 上部分样式 */
.hover-menu {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 30px;
  max-width: 1200px;
  margin: 0 auto;
  justify-items: center;
  align-items: flex-start;
  margin-bottom: 200px;
  margin-top: 50px;
}

.hover-item {
  width: 100px;
  height: 100px;
  text-align: center;
  margin: 20px;
  position: relative;
  overflow: hidden;
  background-color: white;
  border-radius: 35px;
  box-shadow: 0px 7px 10px rgba(0, 0, 0, 0.1);
  padding: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  transition: transform 0.3s ease-in-out;
  cursor: pointer;
  /* font-weight: 700; */
}

.hover-item:hover {
  background: linear-gradient(to bottom, #e098fe, #7877f6);
  color: #fff;
  transform: translateY(-3px);
    color: #fff !important;
  /* 增加hover上浮效果，明确反馈 */
}

/* 图标样式 - 强化hover状态切换的视觉差异 */
.menu-icon {
  font-size: 28px;
  margin-bottom: 8px;
  transition: all 0.2s ease;
  color: #666;
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.menu-icon img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: all 0.2s ease;
}

/* hover时图标明确变化 */
.is-hovered img {

  transform: scale(1.1);
  /* 图标放大，直观反馈 */
  filter: brightness(1.1);
  /* 亮度提升，区分状态 */
}

/* 文字样式 */
.menu-text {
  transition: all 0.3s ease;
  text-align: center;
  margin-top: 6px;
  font-size: 12px;
 
    
}

/* 下部分样式保持不变 */
.click-menu {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  background: white;
  border-radius: 20px;
  box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.1);
  width: 1002px;
  margin: 0 auto;
  height: 71px;
  box-sizing: border-box;
  overflow: hidden;
  position: absolute;
  bottom: 50px;
  left: 50%;
  transform: translateX(-50%);
}

.click-item {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  font-size: 12px;
}

.click-item img {
  width: 30px;
  height: 30px;
}

.click-item.is-active {
  font-size: 12px;
  color: #ffffff;
  background: linear-gradient(to bottom, #38b4cc, #0f7ecd);
}

.click-item:hover {
  font-size: 12px;
  color: #ffffff;
  background: linear-gradient(to bottom, #38b4cc, #0f7ecd);
}

.click-item.is-active .menu-text,
.click-item:hover .menu-text {
  color: #ffffff;
}

/* 响应式布局调整 */
@media (max-width: 768px) {

  .hover-menu,
  .click-menu {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 480px) {

  .hover-menu,
  .click-menu {
    grid-template-columns: repeat(2, 1fr);
  }
}

.footer {
  position: absolute;
  bottom: 20px;
  width: 100%;
  left: 0;
  text-align: center;
  /* 修复footer居中 */
  background-color: #f5f3f8;
}
</style>